<template>
  <a-modal
    :title='currentTitle'
    :width='740'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <div style='max-height: 500px;overflow: auto'>
      <a-spin :spinning='loading'>
        <a-form :form='form' v-bind='formLayout'>
          <a-form-item label='主键' v-show='false'>
            <a-input :disabled='!edit' v-decorator="['id']" />
          </a-form-item>

          <a-form-item label='方案名称'>
            <a-input placeholder='请输入方案名称'
                     :disabled='!edit'
                     v-decorator="['name',{ rules: [
                        { required: true, message: '请输入方案名称!' },
                         { pattern: namePattern,  message: '请输入合法内容(可输入0到50位（中文、字母，数字，下划线，减号)'}]}]" />
          </a-form-item>

          <a-form-item label='方案内容'>
            <a-textarea placeholder='请输入方案内容'
                        :disabled='!edit'
                        v-decorator="['content',{ rules: [ {  required: true, message: '请输入方案内容!' },{max:50,min:1,message:'请输入50位以内的字符'}] }]" />
          </a-form-item>

          <a-form-item label='责任人'>
            <a-input placeholder='请输入责任人'
                     :disabled='!edit'
                     v-decorator="['reponsible',{ rules: [ {  required: true, message: '请输入责任人!' }] }]" />
          </a-form-item>

          <a-form-item label='电话'>
            <a-input placeholder='请输入电话'
                     :disabled='!edit'
                     v-decorator="['phone',{ rules: [ {  required: true, message: '请输入电话!' }] }]" />
          </a-form-item>
        </a-form>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { namePattern } from '@/reges'
import TagSelectOption from '@/components/TagSelect/TagSelectOption'
// 表单字段
const fields = ['id', 'name', 'content', 'reponsible', 'phone']
export default {
  components: { TagSelectOption },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    edit: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    currentTitle() {
      let title = ''
      if (this.edit && (!this.model || !this.model.id)) {
        title = '新增应急信息'
      }
      if (this.edit && this.model && this.model.id) {
        title = '编辑应急信息'
      }
      if (!this.edit) {
        title = '应急信息详情'
      }
      return title
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      }
    }
    return {
      namePattern,
      form: this.$form.createForm(this)
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    validatorPhone(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入设备名称'))
      } else {
        if (!/^\S*$/.test(value)) {
          callback(new Error('请勿输入空格等特殊字符!'))
        } else {
          callback()
        }
      }
    }
  }
}
</script>
<style lang='less' scoped>
.ant-modal-body {
  height: 700px;
  overflow: auto;
}

.avatar {
  width: 200px;
  height: 200px;
}
</style>
